local hx__zhaowei_shi = fk.CreateSkill {
  name = "hx__zhaowei_shi",
  tags = { Skill.Switch },
}


local HX = require "packages.konglong.hxfs2"
local U = require "packages.utility.utility"


hx__zhaowei_shi:addEffect("active", {
  anim_type = "switch",
  prompt = function(self, player)
    if player:getSwitchSkillState(hx__zhaowei_shi.name, false) == fk.SwitchYang then
      return "#hx__zhaowei_shi-yang"
    else
      return "#hx__zhaowei_shi-yin"
    end
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(hx__zhaowei_shi.name, Player.HistoryPhase) < 1 + player:getMark("@hx__zhaowei_shi_skill-turn") and player:getHandcardNum() > 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local cards = player.player_cards[Player.Hand]
    player:showCards(cards)
    if player:getSwitchSkillState(hx__zhaowei_shi.name, true) == fk.SwitchYang then
      if #table.filter(cards, function(id) return Fk:getCardById(id).color == Card.Black end) 
        == #table.filter(cards, function(id) return Fk:getCardById(id).color == Card.Red end) then
          HX.DrawCardFromPile(player.room, player, hx__zhaowei_shi.name, 1)
          HX.DrawCardFromPile(player.room, player, hx__zhaowei_shi.name, 1)
      end
    else
      if #table.filter(cards, function(id) return Fk:getCardById(id).type == Card.TypeBasic end) 
        == #table.filter(cards, function(id) return Fk:getCardById(id).type ~= Card.TypeBasic end) then
        local discards = room:askToDiscard(player, {
          min_num = 2,
          max_num = 2,
          skill_name = hx__zhaowei_shi.name,
          prompt = "#hx__zhaowei_shi-discard",
          skip = true,
          cancelable = true,
          include_equip = true,
        })
        if #discards > 0 then
          room:throwCard(discards, hx__zhaowei_shi.name, player, player)
          local mark = player:getMark("hx__zhaowei_shi_names")
          if type(mark) ~= "table" then
            mark = Fk:getAllCardNames("bt")
            room:setPlayerMark(player, "hx__zhaowei_shi_names", mark)
          end
          local used = player:getTableMark("@$hx__zhaowei_shi-round")
          local all_names, names = {}, {}
          for _, name in ipairs(mark) do
            local card = Fk:cloneCard(name)
            card.skillName = hx__zhaowei_shi.name
            if not player:prohibitUse(card) then
              table.insert(all_names, name)
              if not table.contains(used, name) then
                table.insert(names, name)
              end
            end
          end
          local choices = U.askForChooseCardNames(room, player, names, 1, 1, hx__zhaowei_shi.name, "#hx__zhaowei_shi-ask", all_names)
          room:addTableMark(player, "@$hx__zhaowei_shi-round", choices[1])
          room:askToUseVirtualCard(player, {
            name = choices[1],
            skill_name = hx__zhaowei_shi.name,
            prompt = "#hx__zhaowei_shi-target",
            cancelable = false,
            extra_data = {
              bypass_times = true,
              bypass_distances = true,
              extraUse = true,
            }
          })
        end
      end
    end
  end,
})


return hx__zhaowei_shi